<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<script type="text/javascript">
	function submitAction(action) {
		document.getElementById("actionRequest").value = action;
		document.forms[0].submit();
	}

	function submitDelete(orderId) {
		document.getElementById("actionRequest").value = 'removeItem';
		document.getElementById("orderIdSelected").value = orderId;
		document.forms[0].submit();
	}
</script>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Mercadinho Web - Cadastro de Clientes</title>
</head>
<body>

	<table>
		<tr id="headerContent">
			<td colspan="2"><jsp:include page="header.jsp" /></td>
		</tr>

		<tr id="mainContent">
			<td id="menuContent"><jsp:include page="menu.jsp" /></td>

			<td id="pageContent">

				<form action="order" method="post" name="productForm">

					<input type="hidden" name="action" id="actionRequest" value="" /> 
					<input type="hidden" name="customerId" id="customerId" value='<c:out value="${customer.id }"/>' />
					<input type="hidden" name="orderIdSelected" id="orderIdSelected" value='' />
					

					<table>
						<thead>
							<tr align="center">
								<td colspan="2">Cadastro de Vendas</td>
							</tr>
						</thead>
						<tbody>
							<tr>
								<td>Cliente:</td>
								<td><input type="text" name="name"
									value="${customer.name}" size="50" disabled="disabled" /></td>
							</tr>
							<tr>
								<td>CPF:</td>
								<td><input type="text" name="cpfNumber"
									value="${customer.cpfNumber}" size="50" disabled="disabled" /></td>
							</tr>
							<tr>
								<td>Data Nascimento: </td>
								<td><input type="text" name="price"
									value="${customer.birthDateString}" size="12" disabled="disabled" /></td>
							</tr>
							<tr>
							<td>Produto:</td>
								<td>
								<select name="productIdSelected" id="productIdSelected"  onchange="javascript:submitAction('findProduct')" 
									<c:if test="${ action == 'select' }">
										disabled="disabled"
									</c:if>	
										>
									<c:forEach items="${productList}" var="prod">
										<option  value='<c:out value="${prod.value.product.id }"/>' 
											<c:if test="${product.product.id == prod.value.product.id }"> 
											selected="selected"
											</c:if>
										><c:out value="${prod.value.product.description }"/>
										</option>
									</c:forEach>	
								</select>
								</td>
							</tr>
							
							<tr>
								<td>Qtde Estoque: </td>
								<td><input type="text" name="productQuantity"
									value="${product.quantity}" size="6" disabled="disabled" /></td>
							</tr>
							
							<tr>
								<td>Qtde Pedido: </td>
								<td><input type="text" name="productQuantitySelected" id="productQuantitySelected"
									value="${productQuantitySelected}" size="6"  /></td>
							</tr>
							
							<tr>
							<c:if test="${ action != 'select' }">
								<td colspan="1"><input type="button" name="btnAdd"
									value="Incluir Item no Pedido" onclick="submitAction('add')" /></td>
									<td colspan="1"><input type="button" name="btnAdd"
									value="Fechar Pedido" onclick="submitAction('saveOrder')" /></td>
							</c:if>
							<c:if test="${ action == 'select' }">
								<td colspan="2"><a href="order?action=show&customerId=${customer.id}">Voltar Para Vendas</a></td>
									
							</c:if>
								
							</tr>
							
							<tr>

								<td colspan="2"><font color="red"><b><c:out
												value="${info_message}" /></b></font></td>


							</tr>
						
						
												</tbody>

					</table>



					<table border="1">
						<thead>
							<tr align="center">
								<td>Itens do Pedido de Venda</td>
							</tr>
						</thead>
						<tbody>
							<tr>
								<td>
									<table border="1">
										<thead>
											<tr align="center">
												<td>Codigo Prod</td>
												<td>Produto</td>
												<td>Quantidade</td>
												<td>Valor Unitario</td>
												<td>Valor Total</td>
												<td>Controle</td>
											</tr>
										</thead>

										<tbody>
											<c:forEach items="${sessionScope.order.products}" var="prd">
												<tr align="center">
													<td><c:out value="${prd.value.id}" /></td>
													<td><c:out value="${prd.value.description}" /></td>
													<td><c:out value="${prd.value.quantity}" /></td>
													<td><c:out value="${prd.value.price}" /></td>
													<td><c:out value="${prd.value.totalPrice}" /></td>
													<c:if test="${ action != 'select' }">
														<td><a
															href='javascript:submitDelete(<c:out value="${prd.value.id}"/>)'>Remover</a></td>
													</c:if>	
													<c:if test="${ action == 'select' }">
														<td>Remover</td>
													</c:if>
												</tr>
											</c:forEach>
											<tr align = "left">
												<td colspan = '6'>TOTAL: <c:out value = "${sessionScope.order.totalOrderValue}"/></td>
											</tr>
										</tbody>

									</table>

								</td>

							</tr>


						</tbody>

					</table>



				</form>

			</td>

		</tr>
		<tr id="footerContent">
			<td colspan="2"></td>
		</tr>

	</table>


</body>
</html>